﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Classes.cadastro
{
    public partial class CurrencyEdit : TextBox
    {
        public CurrencyEdit()
        {
            InitializeComponent();
            this.TextAlign = HorizontalAlignment.Right;
        }

        public CurrencyEdit(IContainer container)
        {
            container.Add(this);
            this.TextAlign = HorizontalAlignment.Right;

            InitializeComponent();
        }


        protected override void OnLostFocus(EventArgs e)
        {
            this.Text = formatText();
            this.Value = Convert.ToDouble(this.Text.Replace(Convert.ToChar(_preFix), ' ').Replace(',', ' ').Replace('.', ',').Trim());
            base.OnLostFocus(e);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            this.Value = Convert.ToDouble(this.Text.Replace(Convert.ToChar(_preFix), ' ').Trim());
            WorkingText = this.Text.Replace(Convert.ToChar(_preFix), ' ').Trim();
            //this.Text = this.Value.ToString(getFormat());
            base.OnGotFocus(e);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
           if (!Char.IsDigit(e.KeyChar))
            {
                if (!(e.KeyChar == Convert.ToChar(Keys.Back)) && !(e.KeyChar == 44) && !(e.KeyChar == 46))
                    e.Handled = true;
            }
            base.OnKeyPress(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            //if (this.Text.Trim() == string.Empty)
            //    this.Value = 0;
            //else
            //    this.Value = Convert.ToDouble(this.Text.Replace(Convert.ToChar(_preFix), ' ').Replace(',', ' ').Replace('.', ',').Trim());
            base.OnTextChanged(e);
        }


        private Double _value = 0;

        public Double Value
        {
            get { return _value; }
            set
            {
                try
                {
                    this._value = value;
                    this.Text = this._value.ToString(getFormat());
                    WorkingText = this.Text.Replace(Convert.ToChar(_preFix), ' ').Trim();
                }
                catch (Exception ee)
                {
                    this._value = 0;
                    this.Text = this._value.ToString(getFormat());
                    WorkingText = this.Text.Replace(Convert.ToChar(_preFix), ' ').Trim();
                }
                _value = value;
            }
           
        }

        private string _workingText = "0,00";

        public string WorkingText
        {
            get { return _workingText; }
            private set { _workingText = value; }
        }


        private string _preFix = " ";

        /// <summary>
        /// Contains the prefix that preceed the inputted text.
        /// </summary>
        public string PreFix
        {
            get { return _preFix; }
            set { _preFix = value; }
        }


        private char _thousandsSeparator = ',';

        /// <summary>
        /// Contains the separator symbol for thousands.
        /// </summary>
        public char ThousandsSeparator
        {
            get { return _thousandsSeparator; }
            set { _thousandsSeparator = value; }

        }


        private char _decimalsSeparator = '.';

        /// <summary>
        /// Contains the separator symbol for decimals.
        /// </summary>
        public char DecimalsSeparator
        {
            get { return _decimalsSeparator; }
            set { _decimalsSeparator = value; }
        }


        private int _decimalPlaces = 2;

        /// <summary>
        /// Indicates the total places for decimal values.
        /// </summary>
        public int DecimalPlaces
        {
            get { return _decimalPlaces; }
            set { _decimalPlaces = value; }
        }


        private string getFormat()
        {
            string format = "##0";

            if (DecimalPlaces > 0)
                format += _decimalsSeparator;

            for (int i = 1; i <= DecimalPlaces; i++)
            {
                format += "0";                
            }

            return format;
        }


        public string formatText()
        {
            if (_preFix.Trim() == "")
                _preFix = " ";

            this.Value = Convert.ToDouble(this.Text.Replace(Convert.ToChar(_preFix), ' ').Trim());
            this.Text = this.Value.ToString(getFormat());

            this.WorkingText = 
                this.Text.Replace((_preFix != "") ? _preFix : " ", String.Empty)
                                  .Replace((_thousandsSeparator.ToString() != "") ? _thousandsSeparator.ToString() : " ", String.Empty)
                                           .Replace((_decimalsSeparator.ToString() != "") ? _decimalsSeparator.ToString() : " ", String.Empty).Trim();


            int counter = 1;
            int counter2 = 0;
            char[] charArray = this.WorkingText.ToCharArray();
            StringBuilder str = new StringBuilder();

            for (int i = charArray.Length - 1; i >= 0; i--)
            {
                str.Insert(0, charArray.GetValue(i));
                if (this.DecimalPlaces == 0 && counter == 3)
                {
                    counter2 = counter;
                }

                if (counter == this.DecimalPlaces && i > 0)
                {
                    if (_decimalsSeparator != Char.MinValue)
                        str.Insert(0, _decimalsSeparator);
                    counter2 = counter + 3;
                }
                else if (counter == counter2 && i > 0)
                {
                    if (_thousandsSeparator != Char.MinValue)
                        str.Insert(0, _thousandsSeparator);
                    counter2 = counter + 3;
                }
                counter = ++counter;
            }
            return (this._preFix != "" && str.ToString() != "") ?
               _preFix + " " + str.ToString() :
               (str.ToString() != "") ? str.ToString() : "";
        }

    }
}
